package com.doraro.platform.module.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doraro.platform.common.exception.ApiGlobalException;
import com.doraro.platform.module.device.model.entity.RepairOrder;
import com.doraro.platform.module.device.model.enums.OrderStatus;
import com.doraro.platform.module.device.service.IRepairOrderService;
import com.doraro.platform.module.user.mapper.SysJobMapper;
import com.doraro.platform.module.user.model.entity.SysJob;
import com.doraro.platform.module.user.model.vo.CompleteParam;
import com.doraro.platform.module.user.service.ISysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author doraro
 * @since 2019-06-18
 */
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements ISysJobService {
    @Autowired
    private IRepairOrderService orderService;

    @Override
    public boolean createNewJob(String orderId, String engineerId) {
        Objects.requireNonNull(orderId);
        Objects.requireNonNull(engineerId);
        final SysJob job = new SysJob()
                .setJobStatus(0)
                .setStartTime(LocalDateTime.now())
                .setOrderId(orderId)
                .setUid(engineerId);
        return super.save(job);
    }

    /**
     * TODO:修改自修维修率  现在默认:1.00
     * 工作量算法 现在默认:结束时间-开始时间
     * 单词维修时间算法 现在默认:结束时间-开始时间
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void accpeptJob(String engineerId, String orderId) {
        Objects.requireNonNull(orderId);
        Objects.requireNonNull(engineerId);
        BigDecimal selfRepaireRate = new BigDecimal(1.00);
        final SysJob job = super.lambdaQuery()
                .eq(SysJob::getUid, engineerId)
                .eq(SysJob::getOrderId, orderId)
                .eq(SysJob::getJobStatus, 1)
                .one();
        final LocalDateTime now = LocalDateTime.now();
        final LocalDateTime startTime = job.getStartTime();
        final long days = Duration.between(startTime, now).toDays() + 1;
        super.lambdaUpdate().set(SysJob::getSelfRepaireRate, selfRepaireRate)
                .set(SysJob::getSingleRepairTime, (int) days)
                .set(SysJob::getJobStatus, 2)
                .set(SysJob::getEndTime, now)
                .set(SysJob::getWorkload, (int) days)
                .eq(SysJob::getId, job.getId())
                .update();
    }

    @Override
    public SysJob unCompleteJob(String userId) {
        return super.lambdaQuery()
                .eq(SysJob::getUid, userId)
                .eq(SysJob::getJobStatus, 0)
                .or()
                .eq(SysJob::getJobStatus, 1)
                .one();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer completeJob(String engineerId, CompleteParam param) {
        Objects.requireNonNull(engineerId);
        Objects.requireNonNull(param);
        orderService.completeOrder(param.getOrderId(), engineerId);
        final SysJob repairSolution = param.convert(SysJob.class);
        repairSolution.setJobStatus(1);
        super.updateById(repairSolution);
        return repairSolution.getId();
    }

    @Override
    public Integer updateSolution(String engineerId, CompleteParam solution) {
        final RepairOrder order = orderService.getById(solution.getOrderId());
        if (order == null || !engineerId.equals(order.getEngineerId())
                || !order.getOrderStatus().equals(OrderStatus.FIXED_NOT_CONFIRM)) {
            throw new ApiGlobalException("不存在该订单");
        }
        super.updateById(solution.convert(SysJob.class));
        return solution.getId();
    }
}
